package jk

import "net/http"

func AnyCORS(next HandlerFunc) HandlerFunc {
	return func(c Context) Error {
		c.Response().HeaderSet("Access-Control-Allow-Origin", "*")
		if c.Request().GetMethod() == http.MethodOptions {
			c.Response().HeaderSet("Access-Control-Allow-Headers", "Content-Type,Authorization,Sign,Token")
			c.Response().HeaderSet("Access-Control-Allow-Credentials", "true")
			c.Response().HeaderSet("Access-Control-Allow-Methods", "POST, GET, PUT, PATCH, DELETE, OPTIONS")
			c.Response().WriteStatusCode(http.StatusNoContent)
			return nil
		}
		return next(c)
	}
}
